package ledrpt

import java.text.SimpleDateFormat

class InOutPutController {
    def dynamicQueryService
    def inOutPut() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()-30))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        params.startNo = (pageSize * (pageCurrent - 1)) + 1
        params.endNo = pageSize * pageCurrent

        def seriesData = [[85, 90], [85, 90]]
        def pageSizeList = [10, 20, 50, 100]
        def tableHeadList = []
        def  inOutPut= []
        params.SYS_QUERY_NAME='inOutPutCount'
        def inOutPutCount =dynamicQueryService.queryForList(params)
        def totalCount=inOutPutCount.tatolCount[0]
        params.SYS_QUERY_NAME='packageForm'
        def packageForm=dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME='partNameList'
        def partNameList =dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME='inOutPut'
       inOutPut =dynamicQueryService.queryForList(params)
        if (inOutPut.size() > 0) {
            tableHeadList = inOutPut[0].keySet()
        }
        [startTime:startTime,endTime:endTime,inOutPut:inOutPut,part_name:params.part_name,lot_id:params.lot_id,EDC_SET_NAME:params.EDC_SET_NAME,
         SUBSTRATE_ID1:params.SUBSTRATE_ID1,CUSTOMER_CODE:params.CUSTOMER_CODE,CUSTOMER_PART_ID:params.CUSTOMER_PART_ID,
         CUSTOMER_ORDER:params.CUSTOMER_ORDER,RESERVED1:params.RESERVED1,tableHeadList:tableHeadList,
         inOutPut:inOutPut,packageForm:packageForm,partNameList:partNameList,seriesData: seriesData,,pageSizeList: pageSizeList,totalCount:totalCount
        ]
    }
    def exportExcel() {
        def sysQueryName

        sysQueryName = params.SYS_QUERY_NAME

        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        params.SYS_QUERY_NAME='ftbintitle'
        def ftbintitle =dynamicQueryService.queryForList(params)
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,ftbintitle:ftbintitle])
    }
}
